package pe.gob.osinergmin.interfase_siaf.dac.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import pe.gob.osinergmin.interfase_siaf.dac.dao.CtaCteDao;
import pe.gob.osinergmin.interfase_siaf.dac.domain.CtaCte;

@Repository("ctaCteDao")
public class CtaCteDaoImpl implements CtaCteDao {

    private final Log logger = LogFactory.getLog(getClass());
	
	
	private NamedParameterJdbcTemplate jdbcParameterTemplateData;	
	private JdbcTemplate jdbcTemplateData;	
	
	
	
	@Autowired
	public void SetDataSource(DataSource dataSourceData){
		this.jdbcTemplateData = new JdbcTemplate(dataSourceData);
		this.jdbcParameterTemplateData = new NamedParameterJdbcTemplate(dataSourceData);
		
	}
	
	
	/**DECLARACION DE SQL**/
	private static final String SQL_SELECT_CTA_CTE ="SELECT * FROM cta_cte ";
	private static final String SQL_SELECT_CTA_CTE_PARAMETER ="SELECT * FROM cta_cte WHERE cta_Cte =:ctaCte";
	private static final String SQL_SELECT_CTA_CTE_PARAMETER1 ="SELECT * FROM cta_cte WHERE banco =:banco";
	private static final String SQL_SELECT_CTA_CTE_PARAMETER2 ="SELECT * FROM cta_cte WHERE cta_Cte =:ctaCte AND banco =:banco";
	
	private static final String SQL_SELECT_CTA_CTE_PARAMETER3 ="SELECT COUNT(ano_eje) FROM cta_cte WHERE ano_eje =:anoEje";

	private static final String SQL_SELECT_CTA_CTE_PARAMETER5 ="SELECT COUNT(cta_Cte) FROM cta_cte WHERE cta_Cte =:ctaCte";
	
	@Override
	public List<CtaCte> listarCtaCte() {
		logger.info("Ingresando a listar CtaCte"); 
		RowMapper<CtaCte> lista = ParameterizedBeanPropertyRowMapper.newInstance(CtaCte.class);
		return this.jdbcTemplateData.query(SQL_SELECT_CTA_CTE, lista);
	}
	@Override
	public List<CtaCte> listarCtaCteParameter(CtaCte ctaCte) {
		logger.info("Ingresando a listar CtaCte parameters"); 
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(ctaCte);
		RowMapper<CtaCte> lista = ParameterizedBeanPropertyRowMapper.newInstance(CtaCte.class);
		if(StringUtils.isNoneBlank(ctaCte.getCtaCte()) && StringUtils.isNoneBlank(ctaCte.getBanco())){ 
			return this.jdbcParameterTemplateData.query(SQL_SELECT_CTA_CTE_PARAMETER2, parameters, lista);	
		}else if(StringUtils.isNoneBlank(ctaCte.getCtaCte())){ 
			return this.jdbcParameterTemplateData.query(SQL_SELECT_CTA_CTE_PARAMETER, parameters, lista);	
		}else if(StringUtils.isNoneBlank(ctaCte.getBanco())){
			return this.jdbcParameterTemplateData.query(SQL_SELECT_CTA_CTE_PARAMETER1, parameters, lista);	
		}else{
			return null;
		}			
	}
	
	@Override
	public int verificarCtaCte(CtaCte c) throws Exception {		 
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(c);		
	    return this.jdbcParameterTemplateData.queryForInt(SQL_SELECT_CTA_CTE_PARAMETER5, parameters);	
	}	
	
	@Override
	public int verificarAnoCta(CtaCte c) throws Exception {		 
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(c);		
	    return this.jdbcParameterTemplateData.queryForInt(SQL_SELECT_CTA_CTE_PARAMETER3, parameters);	
	}
	

}
